
package com.example.helloandroid;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class TosActivity extends Activity implements OnClickListener, OnCheckedChangeListener{

	private static final String TAG= "TosActivity";
    public final static String TOS_TEXT= "TosText";
    public final static String EXTERNAL_LAUNCH = "com.motorola.blur.ACTION_LAUNCH_MOTO_SERVICE_AGREEMENT";
    //private static final int READER_REQUEST = 1;
    private Button mNextButton = null;
    private boolean mFromExternalApp = false;
    private boolean mSystemUpdateCase = false;
    private boolean mDisableBack = false;
    private CheckBox mGatheringCheck = null;
    private boolean mFinishActivity = true;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.tos_layout);
        Intent intent = getIntent();
        if (intent != null) {
            String act = intent.getAction();
            if((act != null) && act.equals(EXTERNAL_LAUNCH)) {
                mFromExternalApp = true;
            }
            /*if (intent.getBooleanExtra(HomeActivity.DISABLE_CANCEL, false) == true)*/ {
                mDisableBack = true;
            }
        }
        initWidgets(false);
    }

    @Override
     public void onBackPressed() {
        if (mDisableBack){
           return;
        } else {
           super.onBackPressed();
        }
     }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /*if (requestCode == SetupFSM.ADD_SINGLE_ACCOUNT_DONE)*/ {
            setResult(resultCode);
            finish();
        }
    }

    private void initWidgets(boolean checked) {
        //final Resources res = getResources();

        Button backButton = (Button) findViewById(R.id.back_button);
        Button nextButton = (Button) findViewById(R.id.next_button);

        //SharedPreferences settings = getSharedPreferences(SetupActivity.PREFERENCES, 0);
        mSystemUpdateCase = false; //settings.getBoolean(SetupActivity.SILENT_REG_SYSTEM_UPDATE_REQUEST, false);
        if ((mSystemUpdateCase == true || mFromExternalApp == true) && mDisableBack == false) {
            {
                Log.d(TAG, "Switch backkey to cancel in system update case!!");
            }
            backButton.setText("Cancel");
        }
        mNextButton = nextButton;
        nextButton.setEnabled(checked);

        if (mDisableBack) { // Remove the back button from the nav bar
            backButton.setVisibility(View.GONE);
        }

        TextView tv = (TextView)findViewById(R.id.terms_check_caption_text);
        final MovementMethod method = LinkMovementMethod.getInstance();
        SpannableStringBuilder sb = new SpannableStringBuilder();
        String content = "terms_of_service_check_caption";
        String link = "R.string.tos_link";
        sb.append(content);
        sb.append(link);
        tv.setText(sb);
        tv.setLinksClickable(true);
        tv.setMovementMethod(method);

        tv = (TextView)findViewById(R.id.gathering_check_caption_text);
        content = "R.string.gathering_check_caption";
        link = "R.string.gathering_link";
        sb = new SpannableStringBuilder();
        sb.append(content);
        sb.append(link);
        String content2 = "gathering_check_caption2";
        sb.append(content2);
/*        
        sb.setSpan(new ActivityLinkSpan(R.string.sharing_additional_information,
                R.string.data_analytics_text), content.length(),
                sb.length()-content2.length(), SpannableStringBuilder.SPAN_MARK_POINT);
*/
        tv.setText(sb);
        tv.setLinksClickable(true);
        tv.setMovementMethod(method);

        CheckBox check = (CheckBox)findViewById(R.id.gathering_check);
        // implements a dummy OnClickListener() to make "Touch sounds" work here.
        check.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                return;
            }
        });
        //SharedPreferences prefs = getSharedPreferences(SetupActivity.PREFERENCES, 0);
        //check.setChecked(prefs!= null ? prefs.getBoolean(SetupActivity.DATA_COLLECTION_PREFERENCE, false) : false);
        mGatheringCheck = check;

        check = (CheckBox)findViewById(R.id.tos_check);
        // implements a dummy OnClickListener() to make "Touch sounds" work here.
        check.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                return;
            }
        });
        check.setOnCheckedChangeListener(this);
        check.setChecked(checked);
    }

    /* (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    public void onClick(View view) {
        mFinishActivity = true;
        switch (view.getId()) {
            case R.id.back_button: //back
                this.finish();
                break;

            case R.id.next_button:
                this.finish();
                break;

        }
        if (mFinishActivity == true) {
            this.finish();
        }
        return;
    }

    /* (non-Javadoc)
     * @see android.widget.CompoundButton.OnCheckedChangeListener#onCheckedChanged(android.widget.CompoundButton, boolean)
     */
    public void onCheckedChanged(CompoundButton button, boolean checked) {
        mNextButton.setEnabled(checked);
        mGatheringCheck.setEnabled(checked);
    }
}

class TosActivityTest {
    void doTestShowOnNotification(Context context) {
        //Log.i(TAG," In on receive" + intent.getAction());

        boolean isTablet = true;//SetupUtility.isTabletFlow();
        Notification.Builder builder = new Notification.Builder(context);
        builder.setTicker("Error");
        builder.setContentTitle("title_text");
        builder.setContentText("Press here to view");
        builder.setContentIntent( PendingIntent.getActivity(context, 0,
                          new Intent().setClass(context, TosActivity.class), 0));
        builder.setSmallIcon(R.drawable.icon);

        if (isTablet/*SetupUtility.isTabletFlow()*/) {
            builder.setOngoing(true);
            builder.setAutoCancel(false);
        }
        else {
            builder.setAutoCancel(true);
        }
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(PRIVACY_NOTIFICATION_ID, builder.getNotification());        
    }
    public static final int PRIVACY_NOTIFICATION_ID = 102;    
}
